require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')
require('../lib/iconfont/iconfont.css')

require('../css/login.less')

// 确认验证码是否正确
const axios=require('axios')

// weui-轻提示
require('weui')
const weui= require('weui.js')

let tel=document.querySelector('#tel')
let password=document.querySelector('#password')
let loginBtn=document.querySelector('#loginBtn')
let errMsg=document.querySelector('.err-msg')

window.addEventListener('DOMContentLoaded',function(){
    loginBtn.addEventListener('click',function(){
        let telVal=tel.value
        let passwordVal=password.value

        if(!/^1[3-9][0-9]{9}$/.test(telVal)){
            errMsg.textContent='手机号输入错误'
            return 
        }else if(!/^[a-zA-Z]\w{5,11}$/.test(passwordVal)){
            errMsg.textContent='密码为6~12位'
            return
        }else{
            errMsg.textContent=''
            tel.value=password.value=''

            // 登录账号
            axios.post('http://139.9.177.51:8099/users/login',{
                account: telVal,
                password: passwordVal
            }).then(function(res){
                if(res.data.status===0){
                    // 弹出轻提示：成功
                    localStorage.setItem('userId',res.data.data.user.userId)
                    weui.toast('登录成功', {
                        duration: 1000,
                        callback: function(){ 
                            location.href='./home.html'
                         }
                    });
                }else{
                    // 弹出轻提示：失败
                    weui.alert(res.data.msg)
                }
            })

        }
        
    })
})

